﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IESN.BaseForms;

namespace IESN
{
    public partial class ProductMaterialInfoSettingFrm : EditBaseFrm
    {
        #region 构造函数

        public ProductMaterialInfoSettingFrm()
        {
            InitializeComponent();

            CurrentEntity = new Board();
        }

        #endregion

        #region 行切换

        private void dgvProductMaterialList_SelectionChanged(object sender, EventArgs e)
        {
            CommonHelper<Board> cHelper = new CommonHelper<Board>();

            if (dgvProductMaterialList.SelectedRows != null && dgvProductMaterialList.SelectedRows.Count > 0)
            {
                DataGridViewRow gridrow = dgvProductMaterialList.SelectedRows[0];
                DataRowView row_view = (DataRowView)gridrow.DataBoundItem;
                CurrentEntity = cHelper.FillModel(row_view.Row);

                ControlHelper.SetObjectValueToControl(CurrentEntity, panelContent);
            }
        }

        #endregion

        /// <summary>
        /// 新增合法性判断
        /// </summary>
        /// <returns></returns>
        protected override bool IsInputValid(Control ctl)
        {
            if (string.IsNullOrEmpty(txtCurrDensity_1.Text)||Convert.ToDouble(txtCurrDensity_1.Text) == 0)
            {
                MessageBox.Show("电流密度不能为零！");
                return false;
            }

            if (Convert.ToDouble(txtPlatingArea_A.Text) > 90)
            {
                MessageBox.Show("电镀面积-A 面的面积大于90,不能保存！");
                return false;
            }
            //if ((Convert.ToDouble(txtCurrDensity_1.Text) >= 0.3 && Convert.ToDouble(txtCurrDensity_1.Text)<=60))
            //{
            //    MessageBox.Show(" 铜缸 -》 电流密度在 0.3-60 之间,不能保存！");
            //    return false;
            //}

            return true;
        }
        
        #region 操作完成后处理

        /// <summary>
        /// 新增后处理
        /// </summary>
        protected override void AfterInserted()
        {
            base.AfterInserted();
            InsertGrid();
        }

        /// <summary>
        /// 编辑后处理
        /// </summary>
        protected override void AfterUpDated()
        {
            base.AfterUpDated();
            UpdateGrid();
        }

        /// <summary>
        /// 插入到DataGrid
        /// </summary>
        private void InsertGrid()
        {
            BindingSource bs = dgvProductMaterialList.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;

            CommonHelper<Board> cHelper = new CommonHelper<Board>();
            DataRow newDr = cHelper.FillDataRow(CurrentEntity as Board, dt);
            DataTable cloneTb = dt.Copy();

            cloneTb.Rows.Add(newDr.ItemArray);

            BindingSource newBS = new BindingSource();
            newBS.DataSource = cloneTb;
            dgvProductMaterialList.DataSource = newBS;

            dgvProductMaterialList.Rows[cloneTb.Rows.Count - 1].Selected = true;
            dgvProductMaterialList.Refresh();
        }

        /// <summary>
        /// 更新到DataGrid
        /// </summary>
        private void UpdateGrid()
        {
            BindingSource bs = dgvProductMaterialList.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;
            DataRow[] drs = dt.Select("ID='" + CurrentEntity.ID + "'");

            if (drs != null && drs.Length > 0)
            {
                DataRow dr = drs[0];

                CommonHelper<Board> cHelper = new CommonHelper<Board>();
                DataRow newDr = cHelper.FillDataRow(CurrentEntity as Board, dt);

                drs[0].ItemArray = newDr.ItemArray;
                dgvProductMaterialList.Refresh();
            }
        }

        #endregion
    }
}
